package com.dingqb.myapplication.paint;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

/**
 * 绘制文字
 */
public class DrawView extends View {
    public DrawView(Context context) {
        super(context);
    }

    public DrawView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public DrawView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        int basex = 0;
        int basey = 200;
        String text = "bfgijklpqyz";
        Paint paint = new Paint();
        paint.setColor(Color.RED);
        canvas.drawLine(basex, basey, getMeasuredWidth(), basey, paint);
        paint.setColor(Color.BLUE);
        paint.setTextSize(150);
        canvas.drawText(text, basex, basey, paint);
        Paint.FontMetricsInt fontMetricsInt = paint.getFontMetricsInt();
        paint.setColor(Color.YELLOW);
        canvas.drawLine(basex, basey - Math.abs(fontMetricsInt.top), getMeasuredWidth(), basey - Math.abs(fontMetricsInt.top), paint);
        paint.setColor(Color.GREEN);
        canvas.drawLine(basex, basey - Math.abs(fontMetricsInt.ascent), getMeasuredWidth(), basey - Math.abs(fontMetricsInt.ascent), paint);
        paint.setColor(Color.BLUE);
        canvas.drawLine(basex, basey + Math.abs(fontMetricsInt.descent), getMeasuredWidth(), basey + Math.abs(fontMetricsInt.descent), paint);
        Log.e(TAG, "onDraw: " + fontMetricsInt.toString());
        Log.e(TAG, "onDraw: top==" + fontMetricsInt.top);
        Log.e(TAG, "onDraw: ascent==" + fontMetricsInt.ascent);
        Log.e(TAG, "onDraw: descent==" + fontMetricsInt.descent);
        Log.e(TAG, "onDraw: bottom==" + fontMetricsInt.bottom);
        Log.e(TAG, "onDraw: width==" + getMeasuredWidth());
        Log.e(TAG, "onDraw: width==" + paint.measureText(text));
    }

    public static final String TAG = "dingqb";
}
